package com.dg.test202206140045;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.MenuItem.*;
import android.widget.*;
import com.dg.threec.*;

public class MainActivity extends Activity implements android.view.View.OnClickListener, OnMenuItemClickListener {

    private final static int points[] = new int[] {R.id.point0,R.id.point1,R.id.point2,R.id.point3,R.id.point4,R.id.point5,R.id.point6,R.id.point7,R.id.point8};

    private boolean isAIBlack;

    private boolean isBlack;

    private char map[];

    private void dialogResult(String title) {
        AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false).setTitle(title).setPositiveButton(R.string.restart, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dia, int which) {
                    MainActivity.this.restart();
                }
            }).setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dia, int which) {
                    System.exit(0);
                }
            }).create();
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.show();     
    }

    private void ref() {
        for (int i = 0; i < 9; i++) {
            ImageView iv = this.findViewById(this.points[i]);
            if (this.map[i] == '.') {
                iv.setImageResource(0);
                iv.setEnabled(true);
            } else {
                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(this.map[i] == 'X' ? R.drawable.ic_close : R.drawable.ic_circle_outline);
                iv.setEnabled(false);
            }
        }
    }

    private void restart() {
        this.getActionBar().setSubtitle(R.string.emoji);
        this.isAIBlack = false;
        this.isBlack = true;
        this.map = new char[] {'.','.','.','.','.','.','.','.','.'}.clone();
        this.ref();
        for (int pos_id : points) {
            ImageView iv = MainActivity.this.findViewById(pos_id);
            iv.setEnabled(true);
            iv.setImageResource(0);
        }
        this.findViewById(R.id.board).setVisibility(View.INVISIBLE);
        this.startDialog();
    }

    private void markChess(int index) {
        char mark = this.isBlack ? 'X': 'O';
        this.isBlack = !this.isBlack;
        if (index != -1 && index != 0xff) {
            this.map[index] = mark;
        }
    }

    private void startDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false).setTitle(R.string.app_name).setPositiveButton(R.string.start, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface p1, int p2) {
                    MainActivity.this.getActionBar().setSubtitle(MainActivity.this.isAIBlack ? MainActivity.this.getString(R.string.mach_first): MainActivity.this.getString(R.string.human_first));
                    MainActivity.this.findViewById(R.id.board).setVisibility(View.VISIBLE);
                    if (MainActivity.this.isAIBlack) MainActivity.this.AIGo();
                }
            }).setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dia, int which) {
                    System.exit(0);
                }
            }).setSingleChoiceItems(new CharSequence[]{this.getString(R.string.human_first),this.getString(R.string.mach_first)}, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dia, int which) {
                    MainActivity.this.isAIBlack = which > 0 ? true : false;
                }
            }).create();
        dialog.show();
    }

    private void AIGo() {
        int index = -0xff;
        while (index < -1 || index > 0xff) {
            index = ThreeC.think(new String(this.map), this.isAIBlack ? 'X' : 'O');
        }
        this.markChess(index);
        this.ref();
        index = ThreeC.think(new String(this.map), this.isAIBlack ? 'X' : 'O');
        if (index == 0xff) this.dialogResult(this.getString(R.string.lost_notice)); else if (index == -1) this.dialogResult(this.getString(R.string.draw_notice));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        for (int point:this.points) this.findViewById(point).setOnClickListener(this);
        this.restart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        int index = 0;
        for (int str_id : new int [] {
            R.string.restart,
            R.string.tip,
            R.string.exit
        }) {
            menu.add(0, index, 0, str_id)   
                .setOnMenuItemClickListener(this)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

            index++;
        }

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onClick(View view) {
        for (int i = 0; i < 9; i++) {
            if (view.getId() == this.points[i]) {
                this.markChess(i);
                this.ref();
                this.AIGo();
            }
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem p1) {
        switch (p1.getItemId()) {
            case 0:
                this.restart();
                break;
            case 1:
                this.ref();
                ImageView iv = this.findViewById(this.points[ThreeC.think(new String(this.map), this.isAIBlack ? 'O' : 'X')]);
                iv.setImageResource(R.drawable.ic_star);
                break;
            case 2:
                System.exit(0);
                break;
        }
        return false;
    }
}

